﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using thinger.HeatingControlProject._4.实体类;


namespace thinger.HeatingControlProject._2.数据层
{
    public class SqlDataService
    {
        //定时存储
        public int AddPLCData(PLCData data)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("INSERT INTO HeatingControlDB.dbo.ActualData (InsertTime, PressureIn, PressureOut, TempIn, TempOut, PressureBoiler, LevelBoiler, TempBoiler, LevelTank)");
            sb.Append(" VALUES(@InsertTime,@PressureIn,@PressureOut,@TempIn,@TempOut,@PressureBoiler,@LevelBoiler,@TempBoiler,@LevelTank)");

            SqlParameter[] sqlParameters = new SqlParameter[]
            {
                new SqlParameter("@InsertTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")),
                new SqlParameter("@PressureIn", data.PressureIn),
                new SqlParameter("@PressureOut", data.PressureOut),
                new SqlParameter("@TempIn", data.TempIn),
                new SqlParameter("@TempOut", data.TempOut),
                new SqlParameter("@PressureBoiler", data.PressureBoiler),
                new SqlParameter("@LevelBoiler", data.LevelBoiler),
                new SqlParameter("@TempBoiler", data.TempBoiler),
                new SqlParameter("@LevelTank", data.LevelTank)
            };

            return SqlHelper.ExecuteNonQuery(sb.ToString(), sqlParameters);
        }
        /// <summary>
        /// 根据时间段查询记录
        /// </summary>
        /// <param name="starttime"></param>
        /// <param name="endtime"></param>
        /// <returns></returns>
        public DataSet getActualDataByTime(DateTime starttime,DateTime endtime)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("SELECT InsertTime, PressureIn, PressureOut, TempIn, TempOut, PressureBoiler, LevelBoiler, TempBoiler, LevelTank FROM ActualData");
            stringBuilder.Append(" where inserttime between @starttime and @endtime");
            SqlParameter[] sqlParameters = new SqlParameter[]
            {
                new SqlParameter("@starttime",starttime),
                new SqlParameter("@endtime",endtime)
            };
            return SqlHelper.GetDataSet(stringBuilder.ToString(), null, sqlParameters);
        }
    }
}
